<?php

namespace App\Jobs;

use App\Models\Bill;
use App\Models\DeviceBill;
use App\Models\GlobalBill;
use App\Models\GroupBill;
use App\Models\Order;
use App\Models\Wechat;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;

class CompleteOrderJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $order;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Order $order)
    {
        //
        $this->order = $order;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if($this->order->status !== 'USING') {
            throw new \Exception('订单为使用');
        }
        $usingTime = new Carbon($this->order->using_time);
        if($usingTime->lt(Carbon::now())) {
            throw new \Exception('订单为使用');
        }
        $this->order->complete_time = Carbon::now();
        $this->order->status = 'COMPLETE';
        $this->order->save();
        if ($this->order->paid_price>0) {
            // 创建用户流水
            $data = [
                'origin_order_id'=>$this->order->id,
                'member_id'=>$this->order->member_id,
                'channel'=>$this->order->channel,
                'fee'=>$this->order->paid_price,
                'type'=>'OUTPUT',
                'remark'=>'充电'
            ];
            Bill::create($data);
            // 创建全局流水
            $globalData = [
                'origin_order_id'=>$this->order->id,
                'cate'=>$this->order->channel,
                'people_id'=>$this->order->member_id,
                'fee'=>$this->order->paid_price,
                'remark'=>'充电',
                'type'=>'INPUT',
            ];
            GlobalBill::create($globalData);
            // 创建群组流水
            $groupData = [
                'origin_order_id'=>$this->order->id,
                'device_group_id'=>$this->order->device_group_id,
                'fee'=>$this->order->paid_price,
                'type'=>'INPUT',
                'channel'=>$this->order->channel,
                'remark'=>'充电',
            ];
            GroupBill::create($groupData);
            // 创建设备流水
            $deviceData = [
                'origin_order_id'=>$this->order->id,
                'device_group_id'=>$this->order->device_group_id,
                'fee'=>$this->order->paid_price,
                'type'=>'INPUT',
                'channel'=>$this->order->channel,
                'remark'=>'充电',
                'device_id'=>$this->order->device_id,
            ];
            DeviceBill::create($deviceData);
            // 增加设备订单收益
            $device = $this->order->device;
            if($device->order){
                $device->order->increment('total_sales_amount',$this->order->paid_price);
            }
            // 统计用户支出
            if ($this->order->channel === 'WECHAT'){
                $member = Wechat::find($this->order->member_id);
                $member->statistical->increment('outlay',$this->order->paid_price);
            }
        }
        // 一分钟后开始分成
        DividendJob::dispatch($this->order)->delay(Carbon::now()->addMinutes(1));
    }
}
